package com.wlw.admin.plugindemo;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import android.graphics.drawable.Drawable;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;

import java.io.File;
import java.lang.reflect.Field;


import dalvik.system.DexClassLoader;


public class SecondActivity extends AppCompatActivity {
    private String PLUGIN_NAME = "pluginapp-debug.apk";

    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        imageView = findViewById(R.id.image_view);
        dynamicLoadApk();
        findViewById(R.id.btn_load).setOnClickListener(v -> startTurn());
    }


    private String[] getUninstallApkInfo(String archiveFilePath)
    {
        String[] info = new String[2];
        PackageManager pm = getPackageManager();
        PackageInfo pkgInfo = pm.getPackageArchiveInfo(archiveFilePath, PackageManager.GET_ACTIVITIES);
        if (pkgInfo != null) {
            ApplicationInfo appInfo = pkgInfo.applicationInfo;
            String versionName = pkgInfo.versionName;
            Drawable icon = pm.getApplicationIcon(appInfo);
            String appName = pm.getApplicationLabel(appInfo).toString();
            String pkgName = appInfo.packageName;
            info[0] = appName;
            info[1] = pkgName;
        }
        return info;
    }

    private void dynamicLoadApk() {
        File file = Environment.getExternalStorageDirectory();
        String path = file.getPath() + "/Download/" + PLUGIN_NAME;

        String[] uninstallApkInfo = getUninstallApkInfo(path);
        PluginResources pluginResources = PluginResources.getPluginResources(getResources(), path);
        File optimizedDirectoryFile = getDir("dex", Context.MODE_PRIVATE);
        DexClassLoader dexClassLoader = new DexClassLoader(path, optimizedDirectoryFile.getPath(), null, ClassLoader.getSystemClassLoader());
        try {
            Log.e("tag",uninstallApkInfo[1]);
            Class<?> aClass = dexClassLoader.loadClass(uninstallApkInfo[1] + ".R$mipmap");
            Field field = aClass.getDeclaredField("img_splash");
            int resources = field.getInt(R.mipmap.class);
            imageView.setImageDrawable(pluginResources.getDrawable(resources));

        } catch (ClassNotFoundException | IllegalAccessException | NoSuchFieldException e) {
            e.printStackTrace();
        }

    }


    private void startTurn() {
        Intent intent = new Intent(this, ProxyActivity.class);
        String path = Environment.getExternalStorageDirectory().getPath() + "/Download/" + PLUGIN_NAME;
        intent.putExtra(ProxyActivity.EXTRA_DEX_PATH, path);
        startActivity(intent);
    }
}
